/**
 * Created by baidm in 2022/9/4 on 16:43
 */
class BrowserUtil {
  constructor() {
  }

  /**
   * 滚动到页面顶部
   */
  scrollToTop() {
    window.scrollTo({top: 0, left: 0, behavior: "smooth"});
  }

  /**
   * 滚动到页面底部
   */
  scrollToBottom() {
    window.scrollTo({
      top: document.documentElement.offsetHeight,
      left: 0,
      behavior: "smooth",
    });
  }

  /**
   * 滚动元素到可见区域
   * @param element
   */
  smoothScroll(element) {
    element.scrollIntoView({
      behavior: "smooth",
    });
  }

  /**
   * 全屏显示元素
   * @param element
   */
  launchFullScreen(element) {
    element = element || document.body;
    if (element.requestFullscreen) {
      element.requestFullscreen();
    } else if (element.mozRequestFullScreen) {
      element.mozRequestFullScreen();
    } else if (element.msRequestFullscreen) {
      element.msRequestFullscreen();
    } else if (element.webkitRequestFullscreen) {
      element.webkitRequestFullScreen();
    }
  }

  /**
   * 退出浏览器全屏状态
   */
  exitFullscreen() {
    if (document.exitFullscreen) {
      document.exitFullscreen();
    } else if (document.msExitFullscreen) {
      document.msExitFullscreen();
    } else if (document.mozCancelFullScreen) {
      document.mozCancelFullScreen();
    } else if (document.webkitExitFullscreen) {
      document.webkitExitFullscreen();
    }
  }

  /**
   * 停止冒泡事件
   */
  stopPropagation(event) {
    event = event || window.event;
    if (event.stopPropagation) {
      event.stopPropagation();
    } else {
      event.cancelBubble = true;
    }
  }

  /**
   * 获取浏览器类型及其版本
   * @returns {{type: string, version: number}|{type: string, version: number}|{type: string, version: number}|{type: string, version: number}|{type: string, version: number}|{type: string, version: number}|{type: string, version: number}|{type: string, version: number}}
   */
  getExplorerInfo() {
    let t = navigator.userAgent.toLowerCase();
    return 0 <= t.indexOf("msie")
        ? {
          //ie < 11
          type: "IE",
          version: Number(t.match(/msie ([\d]+)/)[1]),
        }
        : !!t.match(/trident\/.+?rv:(([\d.]+))/)
            ? {
              // ie 11
              type: "IE",
              version: 11,
            }
            : 0 <= t.indexOf("edge")
                ? {
                  type: "Edge",
                  version: Number(t.match(/edge\/([\d]+)/)[1]),
                }
                : 0 <= t.indexOf("firefox")
                    ? {
                      type: "Firefox",
                      version: Number(t.match(/firefox\/([\d]+)/)[1]),
                    }
                    : 0 <= t.indexOf("chrome")
                        ? {
                          type: "Chrome",
                          version: Number(t.match(/chrome\/([\d]+)/)[1]),
                        }
                        : 0 <= t.indexOf("opera")
                            ? {
                              type: "Opera",
                              version: Number(t.match(/opera.([\d]+)/)[1]),
                            }
                            : 0 <= t.indexOf("Safari")
                                ? {
                                  type: "Safari",
                                  version: Number(t.match(/version\/([\d]+)/)[1]),
                                }
                                : {
                                  type: t,
                                  version: -1,
                                };
  }

  /**
   * 获取 cookie
   * @returns {string}
   */
  getCookie() {
    const cookieStr = unescape(document.cookie);
    const arr = cookieStr.split("; ");
    let cookieValue = "";
    for (let i = 0; i < arr.length; i++) {
      const temp = arr[i].split("=");
      if (temp[0] === key) {
        cookieValue = temp[1];
        break;
      }
    }
    return cookieValue;
  }

  /**
   * 删除cookies
   * 把它的过期时间设置为这一刻，它就会立即过期。
   * @param key
   */
  delCookie(key) {
    document.cookie = `${encodeURIComponent(key)}=;expires=${new Date()}`;
  }
}

export default new BrowserUtil()
